<template>
  <div class="content">
    <el-table
      :data="
        tableData.slice((currentPage - 1) * pageSize, currentPage * pageSize)
      "
      style="width: 100%"
      class="el-table__header"
      :header-cell-style="{
        background: '#fafafa',
        borderBottom: '2px solid #e8e8e8',
      }"
    >
      <el-table-column prop="id" label="试题编号" width="120">
      </el-table-column>
      <el-table-column prop="subject" label="学科" width="106">
      </el-table-column>
      <el-table-column prop="catalog" label="目录" width="103">
      </el-table-column>
      <el-table-column prop="questionType" label="题型" width="103">
        <template slot-scope="scope">
          <span>{{ scope.row.questionType | getTixing }}</span>
        </template>
      </el-table-column>
      <el-table-column label="题干" width="280">
        <template slot-scope="scope">
          <span v-html="scope.row.question"></span>
        </template>
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.addDate | formateDate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="difficulty" label="难度" width="103">
        <template slot-scope="scope">
          <span>{{ scope.row.difficulty | getNandu }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="creator" label="录入人" width="103">
      </el-table-column>
      <el-table-column label="操作" class="el_icon">
        <template slot-scope="scope">
          <el-button
            class="EditBtn"
            type="primary"
            icon="el-icon-view"
            circle
            plain
            @click="dialogs(scope.row)"
            title="预览"
          >
          </el-button>
          <el-button
            class="EditBtn"
            type="success"
            icon="el-icon-edit"
            circle
            plain
            @click="updatinfo(scope.row.id)"
            title="修改"
          ></el-button>
          <el-button
            class="EditBtn"
            type="danger"
            icon="el-icon-delete"
            @click="deleteBtn(scope.row)"
            circle
            plain
            title="删除"
          ></el-button>
          <el-button
            class="EditBtn"
            type="warning"
            icon="el-icon-check"
            circle
            plain
            @click="addBtn(scope.row.id)"
            title="加入精选"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block el_pagination">
      <el-pagination
        :page-sizes="[5]"
        :page-size="5"
        :background="true"
        layout="prev, pager, next, sizes, jumper"
        :total="total"
        :current-page="currentPage"
        @current-change="currentChange"
      >
      </el-pagination>
    </div>
    <el-dialog title="题目预览" :visible.sync="outerVisible">
      <el-row :gutter="20">
        <el-col :span="6">【题型】：{{ book.questionType | getTixing }}</el-col>
        <el-col :span="6">【编号】：{{ book.id }}</el-col>
        <el-col :span="6">【难度】：{{ book.difficulty | getNandu }}</el-col>
        <el-col :span="6">【标签】：{{ book.tags }}</el-col>
        <el-col :span="6">【学科】：{{ book.subjectName }}</el-col>
        <el-col :span="6">【目录】：{{ book.directoryName }}</el-col>
        <el-col :span="6">【方向】：{{ book.direction }}</el-col>
      </el-row>
      <hr />
      <div>
        <div>【题干】：</div>
        <div class="tigan" v-html="book.question"></div>
      </div>
      <component :is="component" :book="book"></component>
      <div class="cankao">
        <div>
          <span class="qstitle">【参考答案】：</span>
          <el-button class="qs" @click="videoBtn(book.videoURL)"
            >视频答案预览</el-button
          >
        </div>
      </div>
      <div v-if="isVideoShow" class="VideoPlay">
        <video
          :src="book.videoURL"
          width="300"
          height="180"
          class="VideoPlays"
          poster="../../assets/jiazhuangyoutu.png"
          controls
          autoplay
        ></video>
      </div>
      <hr />
      <div class="jiexi">
        <span class="answerJiexi">【答案解析】：</span>
        <span v-html="book.answer"></span>
      </div>
      <hr />
      <div>【答案备注】：{{ book.remarks }}</div>
      <hr />
      <div slot="footer" class="dialog-footer">
        <el-button @click="outerVisible = false" class="escEdit"
          >关闭</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, remove, choiceAdd, detail } from '@/api/hmmm/questions.js'
import comA from '../components/comA.vue'
import comB from '../components/comB.vue'

export default {
  filters: {
    getTixing (questionType) {
      const Tixing = {
        1: '单选',
        2: '多选',
        3: '简答'
      }
      return Tixing[questionType]
    },
    getNandu (difficulty) {
      const Nandu = {
        1: '简单',
        2: '一般',
        3: '困难'
      }
      return Nandu[difficulty]
    },
    formateDate (time) {
      var datetime = time
      function addDateZero (num) {
        return num < 10 ? '0' + num : num
      }
      const d = new Date(datetime)
      const formatdatetime =
        d.getFullYear() +
        '-' +
        addDateZero(d.getMonth() + 1) +
        '-' +
        addDateZero(d.getDate()) +
        ' ' +
        addDateZero(d.getHours()) +
        ':' +
        addDateZero(d.getMinutes()) +
        ':' +
        addDateZero(d.getSeconds())
      if (datetime) {
        return formatdatetime
      }
    }
  },
  data () {
    return {
      tableData: [],
      total: 0,
      currentPage: 1,
      pageSize: 5,
      data: {
        choiceState: 1
      },
      outerVisible: false,
      book: {},
      component: '',
      isVideoShow: false
    }
  },
  // 计算属性
  computed: {},
  components: {
    comA,
    comB
  },
  methods: {
    async getList () {
      try {
        const { data } = await list()
        this.total = data.items.length
        this.tableData = data.items
        this.$emit('getsum', data.items.length)
      } catch (err) {
        this.$message({
          showClose: true,
          message: '出了个意料之外的错误，不妨喝杯茶稍后重试哦~',
          type: 'error'
        })
      }
    },
    currentChange (value) {
      this.currentPage = value
      this.$emit('currentPage', this.currentPage)
    },
    updatinfo (id) {
      this.$router.push({
        path: '/questions/new',
        query: {
          id
        }
      })
    },
    deleteBtn (info) {
      try {
        this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            await remove(info)
            await this.getList()
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          })
          .catch(() => {
            this.$message('取消删除')
          })
      } catch (err) {
        this.$message({
          showClose: true,
          message: '出了个意料之外的错误，不妨喝杯茶稍后重试',
          type: 'error'
        })
      }
    },
    addBtn (id) {
      try {
        this.$confirm('此操作将该题目加入精选, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        })
          .then(() => {
            this.data.id = id
            choiceAdd(this.data)
            this.$message({
              type: 'success',
              message: '加入精选成功!'
            })
            this.$router.push('/questions/choice')
          })
          .catch(() => {
            this.$message('取消加入精选')
          })
      } catch (err) {
        this.$message({
          showClose: true,
          message: '出了个意料之外的错误，不妨喝杯茶稍后重试',
          type: 'error'
        })
      }
    },
    async dialogs (info) {
      try {
        this.isVideoShow = false
        const { data } = await detail(info)
        this.book = data
        if (this.book.questionType === 1) {
          this.component = comA
        } else if (this.book.questionType === 2) {
          this.component = comB
        } else {
          this.component = ''
        }
        this.outerVisible = true
      } catch (err) {
        this.$message({
          showClose: true,
          message: '出了个意料之外的错误，不妨喝杯茶稍后重试',
          type: 'error'
        })
      }
    },
    videoBtn (videoURL) {
      if (videoURL) {
        this.isVideoShow = true
      } else {
        this.$message({
          showClose: true,
          duration: 1500,
          message: '此处没有视频，不要再点了',
          type: 'warning'
        })
      }
    }
  },
  props: {
    newList: {
      type: Array
    }
  },
  // 监听
  watch: {
    // immediate: true 刚加载就进行监听
    newList (newVal) {
      this.tableData = newVal
      this.total = newVal.length
    },
    tableData (newVal) {
      this.tableData = newVal
    }
  },
  created () {
    this.getList()
  },
  mounted () {}
}
</script>

<style scoped lang="scss">
// ::v-deep
.el-button--medium.is-circle {
  padding: 4px;
}
.el_pagination {
  text-align: right;
  margin-top: 20px;
}
.el-row {
  margin-bottom: 20px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  margin-bottom: 20px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.dialog-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  .escEdit {
    color: #fff;
    background-color: #409eff;
  }
}
.cankao {
  display: flex;
  .qstitle {
    height: 32px;
    line-height: 32px;
  }
  .qs {
    background-color: #f56c6c;
    color: #fff;
  }
}
.VideoPlay {
  margin-top: 30px;
  width: 300px;
  height: 180px;
  .VideoPlays {
    width: 100%;
    height: auto;
  }
}
.jiexi {
  display: flex;
  .answerJiexi {
    line-height: 44.1px;
  }
}
.tigan {
  margin-top: 15px;
  margin-bottom: 15px;
  color: blue;
}
.EditBtn {
  width: 32px;
  height: 32px;
  margin-left: 10px;
}
.EditBtn:nth-child(1) {
  margin-left: 0;
}
</style>
